/**
 * TopicServiceImpl.java 1.0 2007-5-7 上午12:02:30,创建 by yangfan
 */


package org.in4j.bbs.service.impl;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.in4j.bbs.dao.BoardDao;
import org.in4j.bbs.dao.TopicDao;
import org.in4j.bbs.exception.BusinessException;
import org.in4j.bbs.exception.NoSuchBoardException;
import org.in4j.bbs.model.Board;
import org.in4j.bbs.model.Topic;
import org.in4j.bbs.service.TopicService;
import org.in4j.core.dao.support.Pagination;
import org.springframework.util.Assert;

/**
 * 论坛主题管理
 *	TODO 权限验证.
 * @author yangfan
 * @version 1.0 2007-5-7 上午12:02:30,创建
 */
public class TopicServiceImpl implements TopicService
{
	private BoardDao boardDao;
	private TopicDao topicDao;

	public void setBoardDao(BoardDao boardDao)
	{
		this.boardDao = boardDao;
	}

	public void setTopicDao(TopicDao topicDao)
	{
		this.topicDao = topicDao;
	}

	public Pagination<Topic> listTopicsByBoardId(int boardId,int pageNo,int pageSize)
	{
		DetachedCriteria criteria = topicDao.createDetachedCriteria();
		criteria.add(Restrictions.eq("board.id", boardId));
		criteria.addOrder(Order.desc("updateTime"));
		Pagination<Topic> result = topicDao.findPageByCriteria(criteria,pageNo,pageSize);
		return result;
	}

	public Topic getTopicById(Integer id)
	{
		return topicDao.getById(id);
	}

	public void updateTopic(Topic topic) throws BusinessException
	{
		Assert.notNull(topic,"要修改的主题不正确.");
		final Topic originalTopic;
		try
		{
			originalTopic = topicDao.load(topic.getId());
		}
		catch(RuntimeException ex)
		{
			throw new NoSuchBoardException("找不到要修改的主题 " + topic.getTitle(),ex);
		}
		originalTopic.setTitle(topic.getTitle());
		originalTopic.setSubtitle(topic.getSubtitle());
		originalTopic.setContent(topic.getContent());
		originalTopic.setUpdateTime(topic.getUpdateTime());
		originalTopic.setUpdateUser(topic.getUpdateUser());
		topicDao.update(originalTopic);
	}

	public void addNewTopic(final int boardId, final Topic topic) throws BusinessException
	{
		Assert.notNull(topic,"要添加的主题不正确.");

		Board board;
		try
		{
			board = boardDao.load(boardId);
		}
		catch(RuntimeException ex)
		{
			throw new NoSuchBoardException("找不到主题 " + topic.getTitle() + " 所在的论坛版块.",ex);
		}
		board.addTopic(topic);
		board.addTopicCount(1);
		board.setUpdateTime(topic.getUpdateTime());
		board.setUpdateUser(topic.getUpdateUser());
		boardDao.update(board);
		topicDao.create(topic);
//		getHibernateTemplate().execute(new HibernateCallback()
//		{
//
//			public Object doInHibernate(Session session) throws HibernateException, SQLException
//			{
//				Board board = (Board)session.load(Board.class, boardId);
//				board.addTopic(topic);
//				board.addTopicCount(1);
//				board.setUpdateTime(topic.getUpdateTime());
//				board.setUpdateUser(topic.getUpdateUser());
//				session.save(topic);
//				session.update(board);
//				return null;
//			}
//
//		});
	}

	public void removeTopic(int topicId) throws BusinessException
	{
		final Topic topic;
		try
		{
			topic = topicDao.load(topicId);
		}
		catch(RuntimeException ex)
		{
			throw new NoSuchBoardException("找不到要删除的主题 ", ex);
		}
		topic.setStatus(Topic.STATUS_DELETED);
		Board board = topic.getBoard();
		board.addTopicCount(-1);

		boardDao.update(board);
		topicDao.update(topic);
	}
}
